<?php
/* $querystring = parse_url('http://mp3.zing.vn/album/Dut-Tung-Doan-Ruot-Luong-Bich-Huu/ZWZAE6EA.html', PHP_URL_QUERY);
parse_str($querystring, $vars);
echo '<pre>';
print_r($vars);
echo '</pre>'; */



if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
	
	require_once 'libraries/function/validateAjax.php';

	
	$data = array();

	if($_GET['validateTitle'] == 1){
		$validateAjax = new ValidateAjax();
		$validateAjax->validateTitle($_POST['txtTitle']);
		if($validateAjax->isError()){
			$errors = $validateAjax->getErrorMessages();
			echo json_encode($errors);
		}else{
			echo json_encode(array('errors'=>''));
			
		}
		exit();
		
	}

	if($_GET['validateDescription'] == 1){
		$validateAjax = new ValidateAjax();
		$validateAjax->validateDescription($_POST['txtDescription'], array( 'validateAlias'=>'txtDescription', 'validateDescription' => 'Mô tả'));
		if($validateAjax->isError()){
			$errors = $validateAjax->getErrorMessages();
			echo json_encode($errors);
		}else{
			echo json_encode(array('errors'=>''));
			
		}
		exit();
	}

	if($_GET['getStories'] == 1){
		if($_GET['split'] == 1){
			$model = new Model();
			$model->select = '*';
			$model->where="split = 0";
			$model->limit = LIMIT_SPLIT;
			$model->order = 'id ASC';
			$model->getdata('truyen');
			$resultStories = $model->fetchAll();
			
			if($resultStories){
				echo json_encode($resultStories);
				exit();
			}else{
				echo '0';
				exit();
			}
		}
	}
	
	if($_GET['startSplit'] == 1){
			$splitLimit = $_POST['splitLimit'];
			$time = $_POST['time'];
			
			$maxEachTime = 100;
			
			$lastRow = $time * $maxEachTime;
			$reachGoal = $splitLimit - $maxEachTime;
			$startFrom = $splitLimit - MAX_ROWS;
			
			$model = new Model();
			$queryCount = "INSERT INTO " . table_chuong($splitLimit) . 
							" SELECT * 
							FROM truyen_chuong_noidung 
							 WHERE truyen_chuong_id < $splitLimit  
							  AND truyen_chuong_id > $startFrom 
							  ORDER BY truyen_chuong_id ASC
							 LIMIT $lastRow, $maxEachTime";
			
			//echo $queryCount;
			$model->nonquery($queryCount);
			
			if($lastRow == $reachGoal){
				$queryCount = "INSERT INTO " . table_chuong($splitLimit) . 
								" SELECT * 
								FROM truyen_chuong_noidung 
								WHERE truyen_chuong_id = $splitLimit";
				
				//echo $queryCount;
				$model->nonquery($queryCount);
				echo 'het';
			}
			
			exit();
		
	}
	
	/*
	 * Update where getlink_manual = 0 to 1
	 * Bỏ session của thứ tự
	 */
	if($_GET['updateStoryManualStatus'] == 1){
		
		unset($_SESSION['chapts']);
		unset($_SESSION['tap']);
		unset($_SESSION['chapt']);
		unset($_SESSION['last_chapt']);
		
		$model = new Model();
		$data = array('getlink_manual'=>1);
		$model->where = "id = '" . $_POST['story_id'] . "'";
		$model->update('truyen', $data);
		
		exec('rm -rf ' . MONG_PATH . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'truyen' . DIRECTORY_SEPARATOR . $_POST['story_id']);
		
		echo 1;
		exit();
	}
	
	if($_GET['removeAutoScan'] == 1){
		$model = new Model();
		$data=array('getlink_auto'=>0);
		$model->where = "id = '" . $_POST['story_id'] . "'";
		$model->update('truyen', $data);
		exec('rm -rf ' . MONG_PATH . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'truyen' . DIRECTORY_SEPARATOR . $_POST['story_id']);
		echo 1;
		exit();
	}

}




